#include "component_image.h"

struct _ComponentImage
{
    GtkWidget parent_instance;
    GFile *file;
    unsigned int width;
    unsigned int height;
};

enum
{
    PROP_0,
    PROP_FILE,
    PROP_WIDTH,
    PROP_HEIGHT,
    N_PROPERTIES
};

G_DEFINE_TYPE(ComponentImage, component_image, GTK_TYPE_WIDGET)
static GParamSpec *obj_properties[N_PROPERTIES] = {
    NULL,
};

static void component_image_on_draw(GtkWidget *widget, GtkSnapshot *snapshot)
{
    ComponentImage *self = COMPONENT_IMAGE(widget);
    if (self->file == NULL)
    {
        return;
    }
    GBytes *bytes = g_file_load_bytes(self->file, NULL, NULL, NULL);
    GdkTexture *texture = gdk_texture_new_from_bytes(bytes, NULL);
    int texture_width = gdk_texture_get_width(texture);
    int texture_height = gdk_texture_get_height(texture);

    gtk_snapshot_append_texture(snapshot, texture, &GRAPHENE_RECT_INIT(0, 0, texture_width, texture_height));

    g_bytes_unref(bytes);
    g_object_unref(texture);
}

static void component_image_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
    ComponentImage *self = COMPONENT_IMAGE(object);

    switch (property_id)
    {
    case PROP_FILE:
        g_set_object(&self->file, g_value_get_object(value));
        gtk_widget_queue_draw(GTK_WIDGET(self));
        break;
    case PROP_WIDTH:
        self->width = g_value_get_uint(value);
        gtk_widget_queue_draw(GTK_WIDGET(self));
        break;
    case PROP_HEIGHT:
        self->height = g_value_get_uint(value);
        gtk_widget_queue_draw(GTK_WIDGET(self));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
        return;
    }
}

static void component_image_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
    ComponentImage *self = COMPONENT_IMAGE(object);

    switch (property_id)
    {
    case PROP_FILE:
        g_value_set_object(value, self->file);
        break;
    case PROP_WIDTH:
        g_value_set_uint(value, self->width);
        break;
    case PROP_HEIGHT:
        g_value_set_uint(value, self->height);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
        break;
    }
}

static void component_image_class_init(ComponentImageClass *klass)
{
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
    widget_class->snapshot = component_image_on_draw;

    GObjectClass *object_class = G_OBJECT_CLASS(klass);
    object_class->set_property = component_image_set_property;
    object_class->get_property = component_image_get_property;

    obj_properties[PROP_FILE] = g_param_spec_object("file", "File", "File", G_TYPE_FILE, G_PARAM_READWRITE);
    obj_properties[PROP_WIDTH] = g_param_spec_uint("width", "Width", "Width", 0, G_MAXUINT, 0, G_PARAM_READWRITE);
    obj_properties[PROP_HEIGHT] = g_param_spec_uint("height", "Height", "Height", 0, G_MAXUINT, 0, G_PARAM_READWRITE);

    g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties);
}

static void component_image_init(ComponentImage *self)
{
}
